﻿#include "TaskDataDelete.h"
#include <QFile>
#include <QDir>
#include <QFileInfo>

TaskDataDelete::TaskDataDelete(QObject *parent) : MyObject(parent)
{

}

void TaskDataDelete::Start(){
    QThread::sleep(60);

    sqlBase = new SqlBase();
    timer = new QTimer;
    connect(timer, SIGNAL(timeout()), this, SLOT(OnTimer()));
    timer->start(60*60*1000);
   // timer->start(1000);
}

void TaskDataDelete::Stop(){
    timer->stop();
    emit Finished();
}

void TaskDataDelete::OnTimer(){
    MyLog::Info(QString("TaskDataDelete::Start 文件过期天数：%1 数据过期数：%2")
                .arg(globalData->config->system->fileExpireDay)
                .arg(globalData->config->system->dataExpireDay));

    this->DeleteData();
    this->DeleteFile();

    MyLog::Info(QString("TaskDataDelete::End"));
}

void TaskDataDelete::DeleteData(){
    int expireDay = globalData->config->system->dataExpireDay;
    QDateTime time = QDateTime::currentDateTime().addDays(expireDay * -1);

    this->sqlBase->DelQuality(time);
    this->sqlBase->DelSysLog(time);
    this->sqlBase->Deldata_State(time);
    this->sqlBase->Deldata_Sim(time);
}

void TaskDataDelete::DeleteFile(){
    int expireDay = globalData->config->system->fileExpireDay;
    QDateTime time = QDateTime::currentDateTime().addDays(expireDay * -1);

    //删除日志
    QString logName = QString("logs/log-%2.txt").arg(time.toString("yyyy-MM-dd"));
    QString logPath = MyHelper::GetFilePath(logName);
    MyHelper::delFile(logPath);
}
